<%@ Language=VBScript %>
<% 
option explicit 
Response.Expires = -1
Server.ScriptTimeout = 500

Dim data_cookie
data_cookie=Request.Cookies("c1")
Dim loc,ipp,data,datapieces,locpieces

loc=""
ipp=""

Function IPtoloc(ip)

	' Variables
	Dim objXmlHttp
	Dim xmlDoc
	Dim strMainServer
	Dim country
	Dim xmlStr
	xmlStr = "//HostipLookupResultSet/gml:featureMember/Hostip/"

	Dim re
  	Set re = new RegExp

  	re.IgnoreCase = false
  	re.global = false
  	re.Pattern = "[^a-z0-9]"

	' Create XMLHTTP and XML DOM Document objects and set initialize other variable values
	Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
	Set xmlDoc = Server.CreateObject( "MSXML2.DOMDocument.3.0" )
	strMainServer = "http://api.hostip.info/?ip=" & ip

	country = "xx"

	' Query the server
	objXmlHttp.open "GET", strMainServer, False
	objXmlHttp.send

	' If we got an HTTP 200 response, read and parse the XML response
	If objXmlHttp.status = 200 Then
		xmlDoc.loadXML(objXmlHttp.responseXML.xml)
	
		' If there are no parsing errors while reading the XML response, set the country code based on the CountryCode node value
		If xmlDoc.parseError.errorcode = 0 Then
			country = xmlDoc.documentElement.selectSingleNode(xmlStr & "countryAbbrev").text
			
			'check format of fields
			country = mid(lcase(country),1,3)
			if CBool( re.Test(country)) then
				country="xx"
			end if
		End If
	End If

	' Clean up
	Set xmlDoc = Nothing
	Set objXmlHttp = Nothing

	' Return value
	IPtoloc = country & "/" & ip
End Function

data = Request.Cookies(data_cookie)

if(data<>"") then
	datapieces = Split(data,"&")
	if (uBound(datapieces)>13) then
		loc = datapieces(14)
		locpieces = Split(loc,"/")
		if (uBound(locpieces)>1) then
			ipp=locpieces(2)
		end if
	end if
end if

if (loc="" or ipp<>Request.ServerVariables("REMOTE_ADDR")) then
	response.write "<getloc>" & IPtoloc(Request.ServerVariables("REMOTE_ADDR")) & "<getloc>"
else
	response.write "<getloc>" & loc & "<getloc>"
end if

%>